package com.android.superobstacleavoider;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

import com.android.superobstacleavoider.framework.Audio;

public class GLView extends GLSurfaceView {

	// temp
	private float m_x = 0;
	private float m_y = 0;
	//

	private Level m_level;
	private HUD m_hud;
	private Audio m_audio;
	private LookAtCamera m_camera;
	private GameRenderer m_gameRenderer;
	private GameUpdater m_gameUpdater;
	Context m_context;

	public GLView(Context a_context) {
		super(a_context);

		// uncomment this to turn on error-checking and logging
		//setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);

		requestFocus();
		setFocusableInTouchMode(true);

		m_context = a_context;
		m_level = new Level();
		m_hud = new HUD();
		m_camera = new LookAtCamera();
		m_audio = new GameAudio(a_context);
		m_gameRenderer = new GameRenderer(a_context, m_level, m_hud, m_camera,
				m_audio);
		m_gameUpdater = new GameUpdater(a_context, m_level, m_hud, m_camera,
				m_audio);
		setRenderer(m_gameRenderer);
		startGame();
	}

	public void startGame() {
		m_gameUpdater.startThread();
	}

	public Audio getAudio() {
		return m_audio;
	}

	@Override
	public void onPause() {
		super.onPause();
		m_gameUpdater.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
		m_gameUpdater.onResume();
	}

	public boolean onTouchEvent(MotionEvent event) {
		int center = this.getWidth() / 2;
		String inputType = Prefs.getInput(m_context);

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			m_x = event.getX();
			m_y = event.getY();

		}
		// if (event.getAction() == MotionEvent.ACTION_MOVE) {
		// Log.d("TESTING", Prefs.getInput(m_context));
		// if(Prefs.getInput(m_context) == "PRESS")
		// {
		// // if left part of the screen is pressed...
		// if (m_x < center) {
		// m_level.getPlayer().m_isMovingLeft = true;
		// }
		// else if (m_x > center) {
		// m_level.getPlayer().m_isMovingRight = true;
		// }
		// }
		// if(Prefs.getInput(m_context) == "DRAG")
		// {
		// final float xdiff = (m_x - event.getX());
		// final float ydiff = (m_y - event.getY());
		// queueEvent(new Runnable() {
		// public void run() {
		// m_level.setXAngle(m_level.getXAngle() + ydiff);
		// m_level.setYAngle(m_level.getYAngle() + xdiff);
		// }
		// });
		// m_x = event.getX();
		// m_y = event.getY();
		// }
		// }
		// if (event.getAction() == MotionEvent.ACTION_UP) {
		// m_level.getPlayer().m_isMovingLeft = false;
		// m_level.getPlayer().m_isMovingRight = false;
		// }

		// switch (event.getAction() & MotionEvent.ACTION_MASK){
		// case MotionEvent.ACTION_DOWN:
		// case MotionEvent.ACTION_POINTER_DOWN:
		// m_x = event.getX();
		// m_y = event.getY();
		//
		// break;
		// case MotionEvent.ACTION_POINTER_UP:
		// case MotionEvent.ACTION_UP:
		// m_level.getPlayer().m_isMovingLeft = false;
		// m_level.getPlayer().m_isMovingRight = false;
		// break;
		// case MotionEvent.ACTION_MOVE:
		// Log.d("TESTING", Prefs.getInput(m_context));
		// if(Prefs.getInput(m_context) == "PRESS")
		// {
		// // if left part of the screen is pressed...
		// if (m_x < center) {
		// m_level.getPlayer().m_isMovingLeft = true;
		// }
		// else if (m_x > center) {
		// m_level.getPlayer().m_isMovingRight = true;
		// }
		// }
		// if(Prefs.getInput(m_context) == "DRAG")
		// {
		// final float xdiff = (m_x - event.getX());
		// final float ydiff = (m_y - event.getY());
		// queueEvent(new Runnable() {
		// public void run() {
		// m_level.setXAngle(m_level.getXAngle() + ydiff);
		// m_level.setYAngle(m_level.getYAngle() + xdiff);
		// }
		// });
		// m_x = event.getX();
		// m_y = event.getY();
		// }
		// break;
		// }

		// if (inputType.toString() == "DRAG") {
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			final float xdiff = (m_x - event.getX());
			final float ydiff = (m_y - event.getY());
			queueEvent(new Runnable() {
				public void run() {
					//m_level.setXAngle(m_level.getXAngle() + ydiff);
					if(m_level.getPlayer().isValidPosX((m_level.getYAngle() + ydiff) / 15))
						m_level.setYAngle(m_level.getYAngle() + xdiff);
					else
						m_level.setYAngle(-m_level.getPlayer().getPos().X * 15);
				}
			});
			m_x = event.getX();
			m_y = event.getY();
		}
		// }
		return true;
	}
}
